package cn.bs.busuo.front.content.service.impl;

import cn.bs.busuo.common.enumerator.ServiceCode;
import cn.bs.busuo.common.ex.ServiceException;
import cn.bs.busuo.common.pojo.authentication.CurrentPrincipal;
import cn.bs.busuo.front.content.dao.persist.repository.UserHotelArticle;
import cn.bs.busuo.front.content.dao.persist.repository.UserSightArticle;
import cn.bs.busuo.front.content.dao.persist.repository.impl.UserCommentRepositoryImpl;
import cn.bs.busuo.front.content.pojo.entity.ContentHotelComment;
import cn.bs.busuo.front.content.pojo.entity.ContentSightComment;
import cn.bs.busuo.front.content.pojo.param.CommentHotelAddNewParam;
import cn.bs.busuo.front.content.pojo.param.CommentSightAddNewParam;
import cn.bs.busuo.front.content.service.UserCommentService;
import cn.hutool.core.bean.BeanUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author: Feng
 * @since: 2023/12/8
 */
@Service
public class UserCommentServiceImpl implements UserCommentService {

    @Autowired
    private UserCommentRepositoryImpl userCommentRepository;
    @Autowired
    private UserHotelArticle userHotelArticle;
    @Autowired
    private UserSightArticle userSightArticle;

    //酒店文章一级评论
    @Override
    public void commentHotelArticleDetailById(CurrentPrincipal currentPrincipal,Long id, CommentHotelAddNewParam commentHotelAddNewParam) {
        ContentHotelComment contentHotelComment = new ContentHotelComment();
        if (userHotelArticle.selectHotelArticleById(id) == null){
            throw new ServiceException(ServiceCode.ERROR_INSERT,"文章不存在,无法评论");
        }
        BeanUtil.copyProperties(commentHotelAddNewParam,contentHotelComment);
        contentHotelComment.setAuthorId(currentPrincipal.getId());
        contentHotelComment.setAuthorName(currentPrincipal.getUsername());
        contentHotelComment.setArticleId(id);
        contentHotelComment.setParentId(0L);
        contentHotelComment.setUpCount(0);
        contentHotelComment.setDownCount(0);
        contentHotelComment.setCheckState(0);
        contentHotelComment.setIsDisplay(0);
        int i = userCommentRepository.insertHotelComment(contentHotelComment);
        if (i != 1){
            throw new ServiceException(ServiceCode.ERROR_INSERT,"评论失败");
        }
        userHotelArticle.increaseCommentCount(id);
    }

    //酒店文章多级评论
    @Override
    public void commentHotelArticleDetailCommentById(CurrentPrincipal currentPrincipal, Long id, CommentHotelAddNewParam commentHotelAddNewParam) {
        ContentHotelComment contentHotelComment = userCommentRepository.selectHotelCommentById(id);
        //查看评论是否存在
        if (contentHotelComment == null){
            throw new ServiceException(ServiceCode.ERROR_INSERT,"文章评论不存在,无法评论");
        }
        //评论的内容
        BeanUtil.copyProperties(commentHotelAddNewParam,contentHotelComment);
        //评论人信息
        contentHotelComment.setAuthorId(currentPrincipal.getId());
        contentHotelComment.setAuthorName(currentPrincipal.getUsername());
        //文章id
        Long articleId = contentHotelComment.getArticleId();
        contentHotelComment.setArticleId(articleId);
        //设置一级评论Id为二级评论的父级Id
        contentHotelComment.setParentId(contentHotelComment.getId());
        contentHotelComment.setUpCount(0);
        contentHotelComment.setDownCount(0);
        contentHotelComment.setCheckState(0);
        contentHotelComment.setIsDisplay(0);
        //调用评论通用插入方法
        int i = userCommentRepository.insertHotelComment(contentHotelComment);
        if (i != 1){
            throw new ServiceException(ServiceCode.ERROR_INSERT,"评论失败");
        }
        userHotelArticle.increaseCommentCount(articleId);
    }

    //删除酒店文章评论
    @Override
    public void deleteHotelArticleCommentById(CurrentPrincipal currentPrincipal, Long id) {
        //判断该评论是否存在
        ContentHotelComment contentHotelComment = userCommentRepository.selectHotelCommentById(id);
        if (contentHotelComment == null){
            String msg = "该评论不存在";
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND,msg);
        }
        //判断评论作者是否是当前登陆人员
        if (!currentPrincipal.getUsername().equals(contentHotelComment.getAuthorName())){
            String msg = "该评论不是您发布的";
            throw new ServiceException(ServiceCode.ERROR_UPDATE,msg);
        }
        userCommentRepository.updateHotelCommentById(id);
    }

    //景点文章一级评论
    @Override
    public void commentSightArticleDetailById(CurrentPrincipal currentPrincipal, Long id, CommentSightAddNewParam commentSightAddNewParam) {
        ContentSightComment contentSightComment = new ContentSightComment();
        //判断文章是否存在
        if (userSightArticle.selectSightArticleById(id) == null){
         throw new ServiceException(ServiceCode.ERROR_INSERT,"文章不存在,无法评论");
        }
        BeanUtil.copyProperties(commentSightAddNewParam,contentSightComment);
        contentSightComment.setAuthorId(currentPrincipal.getId());
        contentSightComment.setAuthorName(currentPrincipal.getUsername());
        contentSightComment.setArticleId(id);
        contentSightComment.setParentId(0L);
        contentSightComment.setUpCount(0);
        contentSightComment.setDownCount(0);
        contentSightComment.setCheckState(0);
        contentSightComment.setIsDisplay(0);
        int i = userCommentRepository.insertSightComment(contentSightComment);
        if (i != 1){
            throw new ServiceException(ServiceCode.ERROR_INSERT,"评论失败");
        }
        userSightArticle.increaseCommentCount(id);
    }

    //景点文章多级评论
    @Override
    public void addNewSightArticleCommentComment(CurrentPrincipal currentPrincipal, Long id, CommentSightAddNewParam commentSightAddNewParam) {
        ContentSightComment contentSightComment = userCommentRepository.selectSightCommentById(id);
        //查看评论是否存在
        if (contentSightComment == null){
            throw new ServiceException(ServiceCode.ERROR_INSERT,"文章评论不存在,无法评论");
        }
        //评论的内容
        BeanUtil.copyProperties(commentSightAddNewParam,contentSightComment);
        //评论人信息
        contentSightComment.setAuthorId(currentPrincipal.getId());
        contentSightComment.setAuthorName(currentPrincipal.getUsername());
        //文章id
        Long articleId = contentSightComment.getArticleId();
        contentSightComment.setArticleId(articleId);
        //设置一级评论Id为二级评论的父级Id
        contentSightComment.setParentId(contentSightComment.getId());
        contentSightComment.setUpCount(0);
        contentSightComment.setDownCount(0);
        contentSightComment.setCheckState(0);
        contentSightComment.setIsDisplay(0);
        //调用评论通用插入方法
        int i = userCommentRepository.insertSightComment(contentSightComment);
        if (i != 1){
            throw new ServiceException(ServiceCode.ERROR_INSERT,"评论失败");
        }
        userSightArticle.increaseCommentCount(articleId);
    }

    //删除景点文章的评论
    @Override
    public void deleteSightArticleCommentById(CurrentPrincipal currentPrincipal, Long id) {
        //判断该评论是否存在
        ContentSightComment contentSightComment = userCommentRepository.selectSightCommentById(id);
        if (contentSightComment == null){
            String msg = "该评论不存在";
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND,msg);
        }
        //判断评论作者是否是当前登陆人员
        if (!currentPrincipal.getUsername().equals(contentSightComment.getAuthorName())){
            String msg = "该评论不是您发布的";
            throw new ServiceException(ServiceCode.ERROR_UPDATE,msg);
        }
        userCommentRepository.updateSightCommentById(id);
    }
}
